package com.mojo.concurrent.concurrent_01_thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 通过Callable和Future接口使用线程
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2021, 广州九尾信息科技有限公司
 * @date 2021/8/16 16:14
 */
public class CallableDemo01 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        Thread.sleep(5000L);
        System.out.println("当前线程" + Thread.currentThread().getName());
        return 100;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        CallableDemo01 demo01 = new CallableDemo01();
        CallableDemo01 demo02 = new CallableDemo01();
        FutureTask<Integer> task = new FutureTask<>(demo01);
        FutureTask<Integer> task2 = new FutureTask<>(demo02);
        Thread t1 = new Thread(task, "线程1");
        Thread t2 = new Thread(task2, "线程2");
        t2.start();
        t1.start();
        // t1.join();
        // t2.join();
        //调用join方法的线程执行完才能执行后面的线程，本质是wait()方法
        System.out.println("主线程。。。");
        System.out.println(task.get());
        System.out.println(task2.get());
    }

}
